#include "terrianGLSL.h"
#include "FreeImage.h"

#include <cmath>
#include <iostream>
#include <strstream>

using namespace std;

void texSet_terrian(unsigned char* ptexdata)
{
	glActiveTexture(GL_TEXTURE0);

	glGenTextures(1, &AppGloble::terrianTextureNUM);			// Create The Texture

	glBindTexture(GL_TEXTURE_2D, AppGloble::terrianTextureNUM);     // Bind To The Texture ID
	
	//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	////glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	
	//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);	
	//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_NEAREST);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);//GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);//GL_REPEAT);	

	glTexImage2D(GL_TEXTURE_2D,
				0, 
				GL_LUMINANCE_FLOAT32_ATI,
				AppGloble::mapSizex, 
				AppGloble::mapSizey, 
				0, 
				GL_LUMINANCE, 
				GL_FLOAT, 
				ptexdata);

//================================================================================
	FreeImage_Initialise();
	FIBITMAP * dib = FreeImage_Load(FIF_BMP, "./tex_.bmp", BMP_DEFAULT);
	FreeImage_GetWidth(dib),FreeImage_GetHeight(dib);
	dib = FreeImage_ConvertTo24Bits(dib);
	//dib = FreeImage_Rescale(dib, 512, 512, FILTER_BILINEAR);
	BYTE *ptr = FreeImage_GetBits(dib);

	glActiveTexture(GL_TEXTURE1);

	glGenTextures(1, &AppGloble::textureNUM);			// Create The Texture

	glBindTexture(GL_TEXTURE_2D, AppGloble::textureNUM);     // Bind To The Texture ID
	
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);	

	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);  // Pixel Storage Mode (Word Alignment / 4 Bytes)

	glTexImage2D(
		GL_TEXTURE_2D, 
		0, 
		3, 
		FreeImage_GetWidth(dib), 
		FreeImage_GetHeight(dib), 
		0, 
		GL_BGR_EXT, 
		GL_UNSIGNED_BYTE, 
		ptr);
}

void texSet()
{
	glGenTextures(1, &AppGloble::tmpTextureNUM);			// Create The Texture

	glBindTexture(GL_TEXTURE_2D, AppGloble::tmpTextureNUM);     // Bind To The Texture ID

	unsigned char *tmp = new unsigned char[800*600*4];

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
				 800, 600, 0, 
				 GL_RGBA, GL_UNSIGNED_BYTE, tmp);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);	

	delete [] tmp;

	//glCreatePbuffer()
	FreeImage_Initialise();
	FIBITMAP * dib = FreeImage_Load(FIF_BMP, "./tex_.bmp", BMP_DEFAULT);
	FreeImage_GetWidth(dib),FreeImage_GetHeight(dib);
	dib = FreeImage_ConvertTo24Bits(dib);
	//dib = FreeImage_Rescale(dib, 512, 512, FILTER_BILINEAR);
	BYTE *ptr = FreeImage_GetBits(dib);

	glEnable(GL_TEXTURE_2D);
	const GLubyte* theexten = glGetString(GL_EXTENSIONS);
	std::string theextenStr = (char*)theexten;
	if(std::string::npos!=theextenStr.find("GL_ARB_multitexture"))
	{ 
		int nUnits,nSize;
		glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &nUnits);
		glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &nSize);

		//该语句是opengl扩展，所以必须在初始化以后才能调用，10月25号一
		glActiveTexture(GL_TEXTURE0);
		//glActiveTextureARB(GL_TEXTURE0_ARB);
	}
	else
	{
		cout<<endl<<" GL_ARB_multitexture is not supported"<<endl;
	}

	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);  // Pixel Storage Mode (Word Alignment / 4 Bytes)

	// Typical Texture Generation Using Data From The Bitmap
	glGenTextures(1, &AppGloble::textureNUM);			// Create The Texture

	glBindTexture(GL_TEXTURE_2D, AppGloble::textureNUM);     // Bind To The Texture ID

	glTexImage2D(
				GL_TEXTURE_2D, 
				0, 
				3, 
				FreeImage_GetWidth(dib), 
				FreeImage_GetHeight(dib), 
				0, 
				GL_BGR_EXT, 
				GL_UNSIGNED_BYTE,
				ptr);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);	

	//GLfloat zZeroPlane[] = {0.0f, 0.0f, 1.0f, 0.0f};
	//GLfloat xZeroPlane[] = {1.0f, 0.0f, 0.0f, 0.0f};

	//glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	//glTexGenfv(GL_S, GL_OBJECT_PLANE, xZeroPlane);
	//glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	//glTexGenfv(GL_T, GL_OBJECT_PLANE, zZeroPlane);	

	//glEnable(GL_TEXTURE_GEN_S);
	//glEnable(GL_TEXTURE_GEN_T);

	
}

void lightAndMatSet()
{
	//========================加入光照
	glEnable(GL_LIGHTING);

	//glEnable(GL_COLOR_MATERIAL);

	GLfloat ambientlight[]={0.5f,0.5f,0.5f,1.0f};

	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientlight);

	//GLfloat gray[]={0.75f,0.4f,0.9f,1.0f};
	//glMaterialfv(GL_BACK,GL_AMBIENT_AND_DIFFUSE,gray);

	GLfloat amlight[]={0.6f,0.6f,0.6f,1.0f};
	GLfloat dflight[]={0.7f,0.7f,0.7f,1.0f};
	GLfloat specular[] = {0.5f,0.5f,0.5f,1.0f};

	glLightfv(GL_LIGHT0,GL_AMBIENT,amlight);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,dflight);
	glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
	glLightf(GL_LIGHT0,GL_SPOT_CUTOFF, 60.0);

	GLfloat lightpos[]={3.0f,3.0f,3.0f,1.0f};
	glLightfv(GL_LIGHT0,GL_POSITION,lightpos);	

	glEnable(GL_LIGHT0);

	//*材质
	//glEnable(GL_COLOR_MATERIAL);
	GLfloat specref[]={1.0f,1.0f,1.0f,1.0};	
	glMaterialfv(GL_FRONT,GL_AMBIENT,specref);
	glMaterialfv(GL_FRONT,GL_SPECULAR,specref);
	glMateriali(GL_FRONT,GL_SHININESS,128);
	//*/
	glShadeModel(GL_SMOOTH);
}
